local ClassRef = app.mvc.createPanelClass(...)

local BUFF_ZENGYI = 1 --蓝色
local BUFF_JIANYI = 2 --红色
local BUFF_BATTLE = 3 --战斗

local COLOR_ZENGYI = cc.c4b(133,217,227,255)
local COLOR_JIANYI = cc.c4b(252,112,76,255)

local TAG_CD1 = 101
local TAG_CD2 = 102

local string_find = string.find
local string_split = string.split

--local NewguildTips = require("app.newGuild.views.NewguildTips")
local NewRareInfoView = require("app.newBag.views.NewRareInfoView")

function ClassRef.create(node)
    local sprite = ClassRef.extend(node)
    sprite.root = node
    sprite:init()
    return sprite
end


function ClassRef:init(data)
    self.super.init(self, data)
    self.mCdNode = self.root:findChild("Panel")
    self.bg = self.root:findChild("Panel/bg")
    --ICON
    self.icon = self.root:findChild("Panel/icon")
    local zorder = self.icon:getLocalZOrder()
    self.mask = self.root:findChild("Panel/mask")
    self.label = self.root:findChild("Panel/num")

    self.mCdMask1 = self.mCdNode:findChild("battle_buf_cd_1")
    assert(self.mCdMask1)
    self.mCdMask1:setVisible(false)

    self.cdSprite1 = self.mCdNode:getChildByTag(TAG_CD1)
    if not self.cdSprite1 then
        self.cdSprite1 = cc.ProgressTimer:create( self.mCdMask1 )
        self.cdSprite1:setType( cc.PROGRESS_TIMER_TYPE_RADIAL )
        self.cdSprite1:setScaleX(-1)
        self.cdSprite1:setMidpoint(display.ANCHOR_POINTS[1])
        self.cdSprite1:setPosition(self.mCdMask1:getPosition())
        self.mCdNode:addChild(self.cdSprite1)
        self.cdSprite1:setTag(TAG_CD1)
    end

    self.mCdMask2 = self.mCdNode:findChild("battle_buf_cd_2")
    assert(self.mCdMask2)
    self.mCdMask2:setVisible(false)

    self.cdSprite2 = self.mCdNode:getChildByTag(TAG_CD2)
    if not self.cdSprite2 then
        self.cdSprite2 = cc.ProgressTimer:create( self.mCdMask2 )
        self.cdSprite2:setType( cc.PROGRESS_TIMER_TYPE_RADIAL )
        self.cdSprite2:setScaleX(-1)
        self.cdSprite2:setMidpoint(display.ANCHOR_POINTS[1])
        self.cdSprite2:setPosition(self.mCdMask2:getPosition())
        self.mCdNode:addChild(self.cdSprite2)
        self.cdSprite2:setTag(TAG_CD2)
    end
    self.label:setLocalZOrder(self.cdSprite2:getLocalZOrder()+1)
    self.label:setVisible(false)
    self:addTouchEvent()
end

function ClassRef:setType(displaySort)
    if displaySort == BUFF_JIANYI then
        self.cdSprite1:setVisible(false)
        self.cdSprite2:setVisible(true)
        self.cdSprite = self.cdSprite2
        self.label:getVirtualRenderer():setColor(COLOR_JIANYI)
    elseif displaySort == BUFF_ZENGYI then
        self.cdSprite1:setVisible(true)
        self.cdSprite2:setVisible(false)
        self.cdSprite = self.cdSprite1
        self.label:getVirtualRenderer():setColor(COLOR_ZENGYI)
    elseif displaySort == BUFF_BATTLE then
        self.cdSprite1:setVisible(false)
        self.cdSprite2:setVisible(false)
        self.cdSprite = self.cdSprite1
    end
end

local expLimitT = {30,60,99,100}
function ClassRef:refresh()
    if self._data then
        self.root:findChild("Panel/ani"):hide()
        if self._data.tid == 0 then    --战斗经验上限
            local expBattle = UD:getExpDaily()
            local level = UD:getExpRewardLevel()
            local cfg = GD:getBattleExpLimit(level)
            local expLimit = cfg.stageA + cfg.stageB + cfg.stageC
            self._expStatu = expBattle*100 /expLimit
            self.expType = 1
            for i,v in ipairs(expLimitT) do
                if v < self._expStatu then
                    self.expType = i + 1
                end
            end

            if self.expType > 4 then
                self.expType = 4
            end

            self:setType(BUFF_BATTLE)
            local buffIconPath = Res.battleBuffIconPath.."status_0"..tostring(self.expType)..".png"
            self.icon:loadTexture(buffIconPath)
        elseif self._data.tid == 1 then    --战斗经验上限
            self:setType(BUFF_ZENGYI)
            local expLockInfo = self._data.lockInfo
            if self._data.isSecondExpAdd then
                self.icon:loadTexture(string.format("%s%s.png",Res.battleBuffIconPath,expLockInfo.expBuff2_icon))
            else
                self.icon:loadTexture(string.format("%s%s.png",Res.battleBuffIconPath,expLockInfo.expBuff1_icon))
            end
        elseif self._data.tid == 2 then -- 显示挑战赛prepare
            local buffIconPath = string.format(Res.ChallengeBgPath, "challenge_buff")
            self.icon:loadTexture(buffIconPath)
            self.root:findChild("Panel/ani"):show()
        elseif self._data.isTownIcon then
            self.icon:loadTexture(self._data.config:getIcon())
            self.root:findChild("Panel/ani"):show()
            -- self.mapName:setString(cfg:getName())
            -- self.mapIcon:loadTexture(cfg:getIcon())
        else
            local cfg = GD:getBuffConfig(self._data.tid)
            local remainTime = self._data.remainTime

            local buffIconPath = Res.buffIconPath..cfg.icon..".png"
            self.icon:loadTexture(buffIconPath)
            self:setType(BUFF_ZENGYI)
            local percent = remainTime/cfg.duration
            self.cdSprite:setPercentage(100*percent)
        end
    end
end

function ClassRef:addTouchEvent()
    local function touchEvent(sender, eventType)
        if self._data and self._data.isTownIcon then
            if eventType == ccui.TouchEventType.ended then
                local bc = app:getInst("BiddingController")
                bc:openEnterBattleView()
                bc._enterBattleView:refresh(self._data)
            end
            return
        end
        if self._data and self._data.tid == 2 then
            if eventType == ccui.TouchEventType.ended then
                app:sendMsg("PvpChallengeController", "updateChallengePrepare")
            end
            return
        end

        if eventType == ccui.TouchEventType.ended then
            self:showTips()
        end
    end
    self._ui.Panel:addTouchEventListener(touchEvent)
end

function ClassRef:showTips( )
    if self._data then
        if cc.isDead( self._guildTips ) then
            self._guildTips = NewRareInfoView.create()
            self:getParent():addChild( self._guildTips )
            local boundingBox = self:getBoundingBox()
            local posTemp = cc.p( self:convertToWorldSpace( self:getPosition() ) )
            local posTemp2 = cc.p( self:getParent():convertToNodeSpace( posTemp ) )
            self._guildTips:setPosition( cc.p( posTemp2.x - boundingBox.x, posTemp2.y - boundingBox.y ) )
        end

        --改为点击空白处才关掉buff
        display.nodeRemoveForNotHit(self._guildTips, function()
            if not cc.isDead( self._guildTips ) then
                self._guildTips:removeFromParent( true )
                self._guildTips = nil
            end
        end)

        if self._data.tid == 0 then -- 经验（疲劳）buff
            self._guildTips:setData({name=L("state_"..tostring(5-self.expType)),string1 = L("battleExp_"..tostring(5-self.expType)),
                string3 = string.format(L("fatigue_1"),self._expStatu)})
            --self._guildTips:setNameStr(L("state_"..tostring(5-self.expType)))
            --self._guildTips:setContent2Str(L("battleExp_"..tostring(5-self.expType)),L("fatigue_"..tostring(5-self.expType)))
        elseif self._data.tid == 1 then
            local expLockInfo = self._data.lockInfo
            if self._data.isSecondExpAdd then
                dump(expLockInfo.expBuff2)
                self._guildTips:setData({name=L(expLockInfo.expBuff2_name),string3 = string.format(L(expLockInfo.expBuff2_desc),tonumber(expLockInfo.expBuff2)/100)})
            else
                self._guildTips:setData({name=L(expLockInfo.expBuff1_name),string3 = string.format(L(expLockInfo.expBuff1_desc),tonumber(expLockInfo.expBuff1)/100)})
            end



           --self._guildTips:setData({name=L("state_"..tostring(5-self.expType)),string1 = L("battleExp_"..tostring(5-self.expType)),
                --string3 = string.format(L("fatigue_1"))})
        else -- 增减益buff
            local cfg = GD:getBuffConfig(self._data.tid)
            --self._guildTips:setNameStr( L(cfg.name) )

            -- buff描述
            --策划要求改为全部使用mapbuffdesc 省心了
            local str = L(cfg.mapbuffdesc)

            --self._guildTips:setContentStr( str )

            local restTime = TimeUtil.secondsToTime( self._data.remainTime ) -- 剩余时间
            --self._guildTips:setContent2Str( string.format( L("剩余时间：%s"), restTime ) )

            self._guildTips:setData({name=L(cfg.name) ,string3 = str,string1 = string.format( L("lua_code_text_271"), restTime )})

        end
    end
end

return ClassRef
